package com.hzhc.controller.baseinfo;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hzhc.annotation.SysTemLog;
import com.hzhc.common.BaseController;
import com.hzhc.entity.CntrType;
import com.hzhc.enumBean.ResultUtilCodeEnum;
import com.hzhc.service.CntrTypeService;
import com.hzhc.service.SysUserService;
import com.hzhc.utils.ResultUtil;
import com.hzhc.utils.TokenUtil;
import com.hzhc.vo.CntrTypeVo;
import lombok.RequiredArgsConstructor;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 箱类型代码 前端控制器
 * </p>
 *
 * @author 功不唐捐
 * @since 2024-05-23
 */
@RestController
@RequestMapping("/cntrType")
@RequiredArgsConstructor
public class CntrTypeController extends BaseController {

    private final CntrTypeService typeService;
    private final SysUserService userService;

    /**
     * 箱类型代码 list
     * @param request
     * @param vo
     * @return
     */
    @PostMapping("/getCntrTypeList")
    public ResultUtil<List<CntrTypeVo>> getCntrTypeList(HttpServletRequest request, @RequestBody CntrTypeVo vo){
        Page<CntrTypeVo> page = new Page<>(vo.getPageIndex(),vo.getPageSize());
        List<CntrTypeVo> list = typeService.getCntrTypeList(page,vo);
        return ResultUtil.build(list,page.getTotal(),page.getPages(), ResultUtilCodeEnum.SUCCESS);
    }

    /**
     * 箱类型代码 add
     * @param request
     * @param type
     * @return
     */
    @PostMapping("/addCntrType")
    @SysTemLog(operModul = "箱管理-箱类型代码",operType = "ADD",operDesc = "箱类型代码添加",modular="PC")
    public ResultUtil<T> addCntrType(HttpServletRequest request, @RequestBody CntrType type){
        Long userID = TokenUtil.getUserID(request);
        type.setUserId(userID);
        type.setCreateTime(LocalDateTime.now());
        type.setCreateName(userService.getById(userID).getRealname());
        return typeService.addCntrType(type);
    }

    @PostMapping("/checkTypeCode")
    public ResultUtil<T> checkTypeCode(@RequestBody CntrType type){
        Boolean b = typeService.checkTypeCode(type);
        if (b){
            return ResultUtil.ok();
        }
        return ResultUtil.build(ResultUtilCodeEnum.CNTR_TYPE_CODE_EXIST);
    }

    @PostMapping("/updateCntrType")
    @SysTemLog(operModul = "箱管理-箱类型代码",operType = "UPDATE",operDesc = "箱类型代码修改",modular="PC")
    public ResultUtil<T> updateCntrType(HttpServletRequest request,@RequestBody CntrType type){
        Long userID = TokenUtil.getUserID(request);
        type.setUpdateTime(LocalDateTime.now());
        type.setUserId(userID);
        return typeService.updateCntrType(type);
    }

    @RequestMapping("/delCntrType/{id}")
    @SysTemLog(operModul = "箱管理-箱类型代码",operType = "DELETE",operDesc = "箱类型代码删除",modular="PC")
    public ResultUtil<T> delCntrType(@PathVariable Long id){
        return typeService.delCntrType(id);
    }}
